package com.qf.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.qf.Resp.BaseResp;
import com.qf.borrower.Borrower;
import com.qf.dao.UserRespository;
import com.qf.service.RegistryService;
import com.qf.user.TbUser;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import javax.swing.text.html.FormSubmitEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author H
 * @create 2020-11-03 11:10
 * @strive To be whoever you want to be.
 * @Description:
 */
@Service
public class RegistryServiceImpl implements RegistryService {
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    UserRespository userRespository;

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;


    @Override
    public BaseResp registry2(Borrower borrower, String code, String phone) {
        //验证码
        String loginName = borrower.getUsername();
        BaseResp baseResp = new BaseResp();

        Object o = redisTemplate.opsForValue().get(phone);
        System.out.println("o = " + o.toString());
        if (o == null) {
            baseResp.setCode(2998l);
            baseResp.setMessage("验证码以过期");
            return baseResp;
        }
        if (!o.toString().equals(code)) {
            baseResp.setCode(2997l);
            baseResp.setMessage("验证码不匹配");
            return baseResp;
        }
        Borrower byLoginName = userRespository.findByUsername(loginName);
        if (byLoginName != null) {
            baseResp.setCode(2999l);
            baseResp.setMessage("用户名已被占用");
            return baseResp;
        }

        Borrower tbUser1 = userRespository.saveAndFlush(borrower);
        if (tbUser1 == null) {
            baseResp.setCode(2996l);
            baseResp.setMessage("用户注册失败");
            return baseResp;
        }
        baseResp.setCode(2009l);
        baseResp.setMessage("用户注册成功");
        return baseResp;
    }

    //@Override
    public BaseResp sendCode(String phone) {
        /*生成验证码*/
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI4GAEpZYkabC8Fa3Hwr5y", "epQCMqMn61zxzo97zgFdFJhfWPMBrC");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers",phone);
        request.putQueryParameter("SignName", "ABC商城");
        request.putQueryParameter("TemplateCode", "SMS_205392541");

        /*生成随机6位验证码*/
        String code = UUID.randomUUID().toString().substring(0,6);

        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        map.put("code",code);
        //使用redis存储 验证码
        redisTemplate.opsForValue().set(phone,code);
        /*验证码有效时间*/
        redisTemplate.expire(phone,10000, TimeUnit.MINUTES);
        String s = JSONObject.toJSONString(map);

        request.putQueryParameter("TemplateParam", s);
        //System.out.println("map = " + map);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            BaseResp baseResp=new BaseResp();
            baseResp.setCode(200l);
            baseResp.setMessage("手机验证码发送成功");
            return baseResp;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
